package com.vegetable.modules.service.baseData.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vegetable.common.utils.PageUtils;
import com.vegetable.common.utils.Query;
import com.vegetable.modules.entity.baseData.ProductSpec;
import com.vegetable.modules.mapper.baseData.ProductSpecMapper;
import com.vegetable.modules.service.baseData.IProductSpecService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品规格 服务实现类
 * </p>
 *
 * @author wangruijie
 * @since 2024-03-05
 */
@Slf4j
@Service
@AllArgsConstructor
public class ProductSpecServiceImpl extends ServiceImpl<ProductSpecMapper, ProductSpec> implements IProductSpecService {

    /**
     * 商品规格
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String producerId = (String)params.get("producerId");
        IPage<ProductSpec> page = this.page(
                new Query<ProductSpec>().getPage(params),
                new QueryWrapper<ProductSpec>()
                        .eq(StringUtils.isNotBlank(producerId),"producer_id", producerId)
        );

        return new PageUtils(page);
    }

    @Override
    public ProductSpec queryByProductId(String producerId, String specName) {
        return this.getOne(new QueryWrapper<ProductSpec>()
                        .eq("producer_id", producerId)
                        .eq("name", specName));
    }

    @Override
    public List<ProductSpec> queryByProductId(Long producerId) {
        return this.baseMapper.selectList(new QueryWrapper<ProductSpec>()
                .eq("producer_id", producerId));
    }

    @Override
    public List<ProductSpec> queryByProductIds(List<Long> producerIds) {
        return this.baseMapper.selectList(new QueryWrapper<ProductSpec>()
                .in("producer_id", producerIds));
    }
}
